import uicls
import uiutil
import uiconst
import uthread

class OldContainer(uicls._BaseDecoCore, uicls._GetDecoMetaclassInst('triui.UIContainer')):
    __guid__ = 'uicls.OldContainer'
    __persistvars__ = ['_dragging',
     '_dragAllowed',
     '_dragData',
     '_dragLastOver']
    default_name = 'container'
    default_clipChildren = False
    default_pickRadius = 0
    default_opacity = 1.0
    default_align = uiconst.TOALL
    default_state = uiconst.UI_PICKCHILDREN

    def ApplyAttributes(self, attributes):
        uicls._BaseDecoCore.ApplyAttributes(self, attributes)
        self._dragging = False
        self._dragAllowed = False
        self._dragData = None
        if attributes.__gotShape__:
            return 
        self.opacity = attributes.get('opacity', self.default_opacity)
        for attrName in ('clipChildren', 'pickRadius'):
            attr = attributes.get(attrName, getattr(self, ('default_' + attrName)))
            if (attr is not None):
                setattr(self, attrName, attr)




    def _OnClose(self, *args, **kw):
        if self.IsBeingDragged():
            uiutil.Flush(uicore.layer.dragging)
        uicls._BaseDecoCore._OnClose(self, *args, **kw)
        self.GetDragData = None



    def Flush(self):
        for child in self.children[:]:
            if ((child is not None) and (not getattr(child, 'destroyed', 0))):
                child.Close()

        del self.children[:]



    def SetOpacity(self, opacity):
        self.opacity = opacity



    def FindChild(self, childName):
        return uiutil.FindChild(self, childName)



    def GetChild(self, childName):
        return uiutil.GetChild(self, childName)



    def AddSpace(self, axe, amount):
        width = height = 0
        if (axe == uiconst.HORIZONTAL):
            align = uiconst.TOTOP
            height = amount
        elif (axe == uiconst.VERTICAL):
            align = uiconst.TOLEFT
            width = amount
        else:
            raise NotImplementedError
        uicls.Container(align=align, pos=(0,
         0,
         width,
         height), name='_spacer', state=uiconst.UI_DISABLED, parent=self)



    def IsDraggable(self):
        return hasattr(self, 'GetDragData')



    def IsBeingDragged(self):
        return getattr(self, '_dragging', False)



    def _OnMouseDown(self, *args):
        if (self.IsDraggable() and uicore.uilib.leftbtn):
            uicore.Message('DragDropGrab')
            self._dragMouseDown = (uicore.uilib.x, uicore.uilib.y)
            self._dragAllowed = True
        else:
            self._dragAllowed = False



    def _OnMouseUp(self, *args):
        return 



    def _OnMouseMove(self, *args):
        if self.IsBeingDragged():
            self.OnDragMove(self, self._dragData)
            where = uicore.uilib.mouseOver
            whereLast = getattr(self, '_dragLastOver', None)
            if (where is not self):
                if hasattr(where, 'OnDragMove'):
                    uthread.new(where.OnDragMove, self, self._dragData)
                if (whereLast is not where):
                    if hasattr(whereLast, 'OnDragExit'):
                        uthread.new(whereLast.OnDragExit, self, self._dragData)
                    if hasattr(where, 'OnDragEnter'):
                        uthread.new(where.OnDragEnter, self, self._dragData)
            self._dragLastOver = where
            return 
        if ((not self.IsDraggable()) or ((not uicore.uilib.leftbtn) or ((not self._dragAllowed) or (uicore.uilib.mouseTravel < 6)))):
            return 
        uthread.new(self.BeginDrag).context = 'Container::BeginDrag'



    def BeginDrag(self, *args):
        if (getattr(self, 'Draggable_blockDrag', 0) or (not getattr(self, '_dragAllowed', 0))):
            return 
        dragData = self.GetDragData(self)
        if not dragData:
            return 
        self._dragAllowed = False
        self._dragging = True
        self._dragData = dragData
        dragContainer = uicls.DragContainer(name='dragContainer', align=uiconst.ABSOLUTE, idx=0, pos=(0, 0, 32, 32), state=uiconst.UI_DISABLED, parent=uicore.layer.dragging)
        dragContainer.dragData = dragData
        self.KillClickThreads()
        self.DoDrag(dragContainer, dragData)



    def DoDrag(self, dragContainer, dragData):
        if dragContainer.dead:
            return 
        mouseOffset = self.PrepareDrag_(dragContainer, self)
        if self.destroyed:
            return 
        dragContainer.InitiateDrag(mouseOffset)
        if self.destroyed:
            uiutil.Flush(uicore.layer.dragging)
            return 
        dropLocation = uicore.uilib.mouseOver
        if self.IsBeingDragged():
            self._dragAllowed = False
            self._dragging = 0
            self._dragLastOver = None
            uiutil.Flush(uicore.layer.dragging)
            dropLocation = uicore.uilib.mouseOver
            if (getattr(dropLocation, 'OnDropData', None) and self.VerifyDrop(dropLocation, dragData)):
                uthread.new(dropLocation.OnDropData, self, dragData)
            else:
                self.OnDragCanceled_(self, dragData)
        self._dragData = None
        self.OnEndDrag_(self, dropLocation, dragData)



    def PrepareDrag_(self, dragContainer, dragSource, *args):
        return uiutil.PrepareDrag_Standard(dragContainer, dragSource)



    def OnDragCanceled_(self, dragSource, dragData, *args):
        return 



    def OnEndDrag_(self, dragSource, dropLocation, dragData, *args):
        return 



    def OnDragMove(self, dragSource, dragData, *args):
        return 



    def VerifyDrop(self, dragDestination, dragData, *args):
        return True



    def FadeTo(self, opacity = 0.0):
        uthread.new(uicore.effect.CombineEffects, self, opacity=opacity).context = 'Container::FadeTo'




class Container(OldContainer):
    __guid__ = 'uicls.Container'


